<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-form ref="queryFormData" :model="queryFormData" inline size="small">
          <el-form-item label="有效性">
            <el-select v-model="queryFormData.dataFlag" style="width: 150px">
              <el-option :value="0" label="有效"/>
              <el-option :value="1" label="无效"/>
            </el-select>
          </el-form-item>
          <el-button size="small" icon="el-icon-search" @click="search">搜索</el-button>
        </el-form>
      </div>
      <el-table v-loading="loading" :data="tableData" size="mini" style="width: 100%">
        <el-table-column label="歌单图片" width="110" align="center">
          <template slot-scope="{row}">
            <div class="song-list-img">
              <img :src="row.pic" style="width: 100%;"/>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题" width="170" align="center"/>
        <el-table-column prop="stylesStr" label="流派" width="110" align="center"/>
        <el-table-column prop="userName" label="创建人" width="110" align="center">
          <template slot-scope="{row}">
            <p class="el-icon-user"> {{ row.userName }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="createTimeStr" label="创建时间" width="90" align="center"/>
        <el-table-column prop="intro" label="简介" align="center">
          <template slot-scope="{row}">
            <p style="height: 100px; overflow: scroll">{{ row.intro }}</p>
          </template>
        </el-table-column>
        <el-table-column prop="collectTimeStr" label="收藏时间" width="90" align="center"/>
        <el-table-column align="center" label="歌曲" prop="song" width="100">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.collectDataFlag === 1"
                       @click="content(row.id)"
                       icon="el-icon-headset">歌曲
            </el-button>
          </template>
        </el-table-column>
        <el-table-column align="center" label="评论" prop="comment" width="100">
          <template slot-scope="{row}">
            <el-button size="mini" :disabled="row.collectDataFlag === 1"
                       @click="comment(row.id)"
                       icon="el-icon-edit">评论
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="170">
          <template slot-scope="{ row }">
            <el-switch v-model="row.collectDataFlag" :active-value="0"
                       :inactive-value="1" active-text="生效"
                       inactive-text="失效" @change="change(row.id)"/>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination @current-change="handleCurrentChange" background
                       layout="prev, pager, next, total"
                       :current-page="pageNum" :page-size="pageSize" :total="total"/>
      </div>
    </div>
  </div>
</template>

<script>
import {mixin} from '../mixins';
import {collectSongList, delCollect, updateLoveSongDataFlag} from '../api/index';

export default {
  mixins: [mixin],
  data() {
    return {
      tableData: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: true,
      queryFormData: {
        userId: this.$route.query.userId,
        dataFlag: 0
      },
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    getData() {
      this.loading = true;
      const {queryFormData, pageNum, pageSize} = this;
      collectSongList(queryFormData, pageNum, pageSize).then(res => {
        this.loading = false;
        const {data, total, success} = res;
        if (!success) {
          this.$message.error('查询失败');
          this.tableData = [];
          return;
        }
        this.tableData = data.map(e => {
          return {
            ...e,
            toggle: false
          }
        });
        this.total = total;
      })
    },
    search() {
      this.getData();
    },
    handleCurrentChange(currentPageNum) {
      this.pageNum = currentPageNum;
      this.getData();
    },
    change(id) {
      let data = {
        songListId: id,
        userId: this.$route.query.userId
      };
      updateLoveSongDataFlag(data).then(res => {
        const {success} = res;
        if (success) {
          this.$message.success("操作成功");
          this.getData();
        } else {
          this.$message.error("操作失败");
        }
      });
    },
    comment(id) {
      this.$router.push({path: "/Comment", query: {songListId: id}});
    },
    content(id) {
      this.$router.push({path: "/Content", query: {songListId: id}});
    },
  }
}

</script>

<style scoped>
.song-list-img {
  width: 100%;
  height: 90px;
  margin-top: 5px;
  margin-bottom: 5px;
  overflow: hidden;
}
</style>